<?php 
/**
 * 列表分类控制器
 */
 class CategoryController extends CommonController{
	public $model;
	public function __init(){
		parent::__init();
	}
	
	/**
	 * 首页
	 * 
	 */
	 public function index(){
/*	 	//点击分类获得分类的cid
		$cid = Q('get.cid');
		//点击品牌的话获得品牌的bid
		$bid = Q('get.bid');
		//找到所有品牌
		$brands = K('Brands')->where('is_show=1')->limit(24)->all();
		//p($brands);die;
        $this->assign('brands',$brands);
		//获得当前分类
		$cate1 = K('category')->where("cid={$cid}")->find();
		//p($cate1);
        $this->assign('cate1',$cate1);
		//获得所有分类
		$cateDate = K('Category')->limit(24)->all();
		//找到该分类名称以及其子分类
		$sonCid = K('Category')->getSon($cateDate,$cid);
		$sonCid[] = $cid;
		//p($sonCid);
		//显示出来的分类里不能有自己和自己的子级
		$cateDate = K('Category')->where("cid IN(". implode(',',$sonCid).")")->all();
		//p($cateDate);
		$this->assign('cateDate',$cateDate);
		
		//找到该分类下面的所有产品,并且组合数组将产品的属性一起输出
		$goods = K('Goods')->where("category_cid IN(". implode(',', $sonCid).") AND is_show=1")->all();
        foreach ($goods as $k => $v) {
            $goods[$k]['attr1'] = M()->join('__goods_attr__ ga JOIN __type_attr__ ta ON ga.type_attr_taid=ta.taid')->where("goods_gid={$v['gid']} AND class=0")->all();
        }
//         p($goods);
        $this->assign('goods',$goods);

        // 面包屑导航
        $data = K('Category')->all();
        // 获得父级分类
        $fatherCate = $this->getFather($data,$cid);
        
        // 数组反转
        $fatherCate = array_reverse($fatherCate);
        //p($fatherCate);
        $this->assign('fatherCate',$fatherCate);
		//顶级分类
		$this->topCate();
		
		
		*/

		
		
		////////////////////////////////////////////////////////////////////
   	//获得顶级分类

    	$topCate = K('Category')->where('pid=0')->all();
    	//分配顶级分类变量
		$this->assign('topCate',$topCate);
		//通过分类获得商品gid		
		$cid = Q('get.cid',0,'intval');
		//获得当前分类的子集cid
		$cids = $this->_getSon($cid);
		$gids = K('Goods')->where("category_cid in(" . implode(',', $cids) . ")")->getField('gid',true);
		//如果分类下面有商品的时候
		if($gids){
			//通过商品gid得到属性
			$attr = K('GoodsAttr')->where("goods_gid in(" . implode(',', $gids) . ")")->group('attr')->all();
			//以下两个临时数组为重组列表页需要的属性
			$tempAttr = array();
			foreach ($attr as $v) {
				$tempAttr[$v['type_attr_taid']][] = $v;
			}
//			p($tempAttr);
			$tempFinalAttr = array();
			//组合有名称name的属性
			foreach ($tempAttr as $taid => $value) {
				$tempFinalAttr[] = array(
					'name' => K('Type_attr')->where("taid=" . $taid)->getField('taname'),
					'value' => $value
				);
			}
			$this->assign('tempFinalAttr',$tempFinalAttr);
			//List/index/cid/1/param/0_0_0_0_0
			$num = count($tempFinalAttr);
			$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
			$this->assign('param',$param);
			//进行筛选
			foreach ($param as $v) {
				if($v){
					$filterGids[] = M()->join('__goods_attr__ g1 JOIN __goods_attr__ g2 ON g1.attr=g2.attr')->where("g1.gaid={$v}")->getField('g2.goods_gid',true);
				}
			}
			//如果有筛选的gid
			if(isset($filterGids)){
				//取交集
				$finalGids = $filterGids[0];
				foreach ($filterGids as $v) {
					$finalGids = array_intersect($finalGids, $v);
				}
				//属性获得交集gid之后，再和分类下面的商品gid再取交集
				$finalGids = array_intersect($finalGids,$gids);
				
			}else{
				//全部不限的时候，就是当前分类下面的所有商品gid
				$finalGids = $gids;
			}
		}else{
			//如果分类下面没有商品,把gid赋值为空数组
			$finalGids = array();
		}
		//最后的gid O(∩_∩)O~
//		p($finalGids);
		if($finalGids){
			$goods = K('Goods')->where("gid in (" . implode(',', $finalGids) . ")")->all();
		}else{
			$goods = NULL;
		}
//		p($goods);
		$this->assign('goods',$goods);
		
   		$cate1 = K('category')->where("cid={$cid}")->find();
		//p($cate1);
        $this->assign('cate1',$cate1);
	 	$this->display();

	 }

	////获得子分类
	private function _getSon($cid){
		$data = K('Category')->all();
		$cids = $this->_getSonCid($data,$cid);
		$cids[] = $cid;
		return $cids;
	}
	
	
	//获得子分类ID
	private function _getSonCid($data,$cid){
		$temp = array();
		foreach ($data as $v) {
			if($v['pid'] == $cid){
				$temp[] = $v['cid'];
				$temp = array_merge($temp,$this->_getSonCid($data, $v['cid']));
			}
		}
		return $temp;
	}
	 
	 
	  
	
 }


 ?>